package com.yic.module.qms.service.inspectinfo;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.qms.controller.admin.inspectinfo.vo.*;
import com.yic.module.qms.dal.dataobject.inspectinfo.InspectInfoDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.qms.convert.inspectinfo.InspectInfoConvert;
import com.yic.module.qms.dal.mysql.inspectinfo.InspectInfoMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.qms.enums.ErrorCodeConstants.*;

/**
 * 检验单身 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class InspectInfoServiceImpl implements InspectInfoService {

    @Resource
    private InspectInfoMapper inspectInfoMapper;

    @Override
    public Long createInspectInfo(InspectInfoCreateReqVO createReqVO) {
        // 插入
        InspectInfoDO inspectInfo = InspectInfoConvert.INSTANCE.convert(createReqVO);
        inspectInfoMapper.insert(inspectInfo);
        // 返回
        return inspectInfo.getId();
    }

    @Override
    public void createInspectInfo(Collection<InspectInfoCreateReqVO> createReqVOS) {
        List<InspectInfoDO> inspectInfoDOS = InspectInfoConvert.INSTANCE.convertList03(createReqVOS);
        inspectInfoMapper.insertBatch(inspectInfoDOS);
    }

    @Override
    public void updateInspectInfo(InspectInfoUpdateReqVO updateReqVO) {
        // 校验存在
        validateInspectInfoExists(updateReqVO.getId());
        // 更新
        InspectInfoDO updateObj = InspectInfoConvert.INSTANCE.convert(updateReqVO);
        inspectInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteInspectInfo(Long id) {
        // 校验存在
        validateInspectInfoExists(id);
        // 删除
        inspectInfoMapper.deleteById(id);
    }

    private void validateInspectInfoExists(Long id) {
        if (inspectInfoMapper.selectById(id) == null) {
            throw exception(INSPECT_INFO_NOT_EXISTS);
        }
    }

    @Override
    public InspectInfoDO getInspectInfo(Long id) {
        return inspectInfoMapper.selectById(id);
    }

    @Override
    public List<InspectInfoDO> getInspectInfoList(Collection<Long> ids) {
        return inspectInfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InspectInfoDO> getInspectInfoPage(InspectInfoPageReqVO pageReqVO) {
        return inspectInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InspectInfoDO> getInspectInfoList(InspectInfoExportReqVO exportReqVO) {
        return inspectInfoMapper.selectList(exportReqVO);
    }

    @Override
    public List<InspectInfoRespVO> getInspectInfoListByInspectId(Long id) {
        List<InspectInfoDO> inspectInfoDOS = inspectInfoMapper.selectListByInspectId(id);
        return InspectInfoConvert.INSTANCE.convertList(inspectInfoDOS);
    }

}
